package com.jy.rhin.support.interceptor.convertor;

import cn.org.chiss.rhin._2015.registration.terminology.RetrieveConceptMapRequest;
import cn.org.chiss.rhin._2015.registration.terminology.RetrieveConceptMapResponse;
import cn.org.chiss.rhin._2015.registration.terminology.RetrieveValueSetRequest;
import cn.org.chiss.rhin._2015.registration.terminology.RetrieveValueSetResponse;
import com.jy.rhin.application.enums.*;
import com.jy.rhin.sdk.dto.audit.AuditSourceDto;
import com.jy.rhin.sdk.dto.audit.CodedValueTypeDto;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 术语注册服务-值集映射获取接口，打包审计信息
 */
@Service("AuditInfoConverter4TerminologyRetrieveConceptMap")
public class AuditInfoConverter4TerminologyRetrieveConceptMap extends BaseAuditInfoConverter<RetrieveConceptMapRequest, RetrieveConceptMapResponse> {

    public AuditInfoConverter4TerminologyRetrieveConceptMap() {
        super(EventActionCodeEnum.READ,
                ServiceCodeEnum.TERMINOLOGY_REGISTRATION,
                EventTypeCodeEnum.RETRIEVE_CONCEPT_MAP,
                true,
                ParticipantObjectIDTypeCodeEnum.SEARCH_CRITERIA,
                ParticipantObjectTypeCodeRoleEnum.RESOURCE,
                ParticipantObjectTypeCodeEnum.SYSTEM_OBJECT,
                ParticipantObjectDataLifeCycleEnum.ACCESS_USE);
    }

    /**
     * 审计源（审计源为事件目标）
     */
    @Override
    List<AuditSourceDto> buildAuditSource() {
        AuditSourceDto auditSource = new AuditSourceDto();
        // TODO 从安全节点验证服务，获取节点OID
        auditSource.setBiaoShi("terminology-service");
        // TODO: 从SOAP报文头获取<TO>节点，做为审计源网址
        auditSource.setWangZhi("http://localhost");
        CodedValueTypeDto auditSourceTypeCode = new CodedValueTypeDto();
        auditSourceTypeCode.setCodeSystem(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getCode());
        auditSourceTypeCode.setCodeSystemName(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getName());
        auditSourceTypeCode.setCode(AuditSourceTypeCodeEnum.WEB_SERVER_PROCESS.getCode());
        auditSourceTypeCode.setDisplayName(ServiceCodeEnum.TERMINOLOGY_REGISTRATION.getName());
        auditSource.setLeiXing(auditSourceTypeCode);
        return Collections.singletonList(auditSource);
    }

    @Override
    AuditParticipantUserDesc buildSourceUser(RetrieveConceptMapRequest request, RetrieveConceptMapResponse response) {
        return new AuditParticipantUserDesc("client-1", "客户端");
    }

    @Override
    AuditParticipantUserDesc buildOriginatorUser(RetrieveConceptMapRequest request, RetrieveConceptMapResponse response) {
        return new AuditParticipantUserDesc("system", "admin");
    }

    @Override
    AuditParticipantUserDesc buildTargetUser(RetrieveConceptMapRequest request, RetrieveConceptMapResponse response) {
        return new AuditParticipantUserDesc("terminology-service", ServiceCodeEnum.TERMINOLOGY_REGISTRATION.getName());
    }

    @Override
    String getObjectId(RetrieveConceptMapRequest request, RetrieveConceptMapResponse response) {
        if (Objects.nonNull(request)) {
            return request.getId().getValue();
        }
        if (Objects.nonNull(response)
                && Objects.nonNull(response.getConceptMap())
                && Objects.nonNull(response.getConceptMap().getId())) {
            return response.getConceptMap().getId().getValue();
        }
        return "";
    }
    
    @Override
   	public boolean isMatch(String method, Object data) {
   		return "ConceptMapProviderPortImpl.retrieveConceptMap".equals(method);
   	}

}
